/*
 * SafeNativeMethods.cs
 *
 * Contains Safe P/Invoke Methods.
 *
 * Copyright (c) 2005, dnAnalytics. All rights reserved.
 */

#region Using Directives
using System.Runtime.InteropServices;
using System.Security;
using dnAnalytics.Math;
#endregion

namespace dnAnalytics.LinearAlgebra.Native.Mkl
{
    [SuppressUnmanagedCodeSecurity]
    internal static class SafeNativeMethods
    {
        private const string dllName = "mkl.dll";

        #region PInvoke calls

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double cblas_ddot(int n,  double[] x, int incX,  double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zdotu_sub(int n,  Complex[] x, int incX,  Complex[] y, int incY, ref Complex dotu);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zdotc_sub(int n,  Complex[] x, int incX,  Complex[] y, int incY, ref Complex dotc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double cblas_dnrm2(int n,  double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double cblas_dasum(int n,  double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double cblas_dznrm2(int n,  Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double cblas_dzasum(int n,  Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern int cblas_idamax(int n,  double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern int cblas_izamax(int n,  Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dswap(int n, [Out] double[] x, int incX, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dcopy(int n,  double[] x, int incX, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_daxpy(int n, double alpha,  double[] x, int incX, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_drotg(ref double a, ref double b, out double c, out double s);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zswap(int n, [Out] Complex[] x, int incX, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zcopy(int n,  Complex[] x, int incX, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zaxpy(int n, ref Complex alpha,  Complex[] x, int incX, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zrotg(ref Complex a, ref Complex b, out Complex c, out Complex s);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_drotmg(ref double d1, ref double d2, ref double x1, ref double y1, [Out] double[] p);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_drot(int n, [Out] double[] x, int incX, [Out] double[] y, int incY, double c, double s);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_drotm(int n, [Out] double[] x, int incX, [Out] double[] y, int incY, [Out] double[] p);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zdrot(int n, [Out] Complex[] x, int incX, [Out] Complex[] y, int incY, double c, double s);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dscal(int n, double alpha, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zscal(int n, ref Complex alpha, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zdscal(int n, double alpha, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dgemv(BlasOrderType order, BlasTransType transA, int m, int n, double alpha,  double[] a, int lda,  double[] x, int incX, double beta, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dgbmv(BlasOrderType order, BlasTransType transA, int m, int n, int kl, int ku, double alpha,  double[] a, int lda,  double[] x, int incX, double beta, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtrmv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  double[] a, int lda, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtbmv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n, int k,  double[] a, int lda, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtpmv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  double[] ap, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtrsv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  double[] a, int lda, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtbsv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n, int k,  double[] a, int lda, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtpsv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  double[] ap, [Out] double[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zgemv(BlasOrderType order, BlasTransType transA, int m, int n, ref Complex alpha,  Complex[] a, int lda,  Complex[] x, int incX, ref Complex beta, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zgbmv(BlasOrderType order, BlasTransType transA, int m, int n, int kl, int ku, ref Complex alpha,  Complex[] a, int lda,  Complex[] x, int incX, ref Complex beta, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztrmv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  Complex[] a, int lda, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztbmv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n, int k,  Complex[] a, int lda, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztpmv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  Complex[] ap, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztrsv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  Complex[] a, int lda, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztbsv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n, int k,  Complex[] a, int lda, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztpsv(BlasOrderType order, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int n,  Complex[] ap, [Out] Complex[] x, int incX);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsymv(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  double[] a, int lda, double[] x, int incX, double beta, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsbmv(BlasOrderType order, BlasUpLoType uplo, int n, int k, double alpha,  double[] a, int lda,  double[] x, int incX, double beta, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dspmv(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  double[] ap,  double[] x, int incX, double beta, [Out] double[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dger(BlasOrderType order, int m, int n, double alpha,  double[] x, int incX,  double[] y, int incY, [Out] double[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsyr(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  double[] x, int incX, [Out] double[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dspr(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  double[] x, int incX, [Out] double[] ap);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsyr2(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  double[] x, int incX,  double[] y, int incY, [Out] double[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dspr2(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  double[] x, int incX,  double[] y, int incY, [Out] double[] a);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zhemv(BlasOrderType order, BlasUpLoType uplo, int n, ref Complex alpha,  Complex[] a, int lda,  Complex[] x, int incX, ref Complex beta, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zhbmv(BlasOrderType order, BlasUpLoType uplo, int n, int k, ref Complex alpha,  Complex[] a, int lda,  Complex[] x, int incX, ref Complex beta, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zhpmv(BlasOrderType order, BlasUpLoType uplo, int n, ref Complex alpha,  Complex[] ap,  Complex[] x, int incX, ref Complex beta, [Out] Complex[] y, int incY);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zgeru(BlasOrderType order, int m, int n, ref Complex alpha,  Complex[] x, int incX,  Complex[] y, int incY, [Out] Complex[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zgerc(BlasOrderType order, int m, int n, ref Complex alpha,  Complex[] x, int incX,  Complex[] y, int incY, [Out] Complex[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zher(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  Complex[] x, int incX, [Out] Complex[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zhpr(BlasOrderType order, BlasUpLoType uplo, int n, double alpha,  Complex[] x, int incX, [Out] Complex[] a);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zher2(BlasOrderType order, BlasUpLoType uplo, int n, ref Complex alpha,  Complex[] x, int incX,  Complex[] y, int incY, [Out] Complex[] a, int lda);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zhpr2(BlasOrderType order, BlasUpLoType uplo, int n, ref Complex alpha,  Complex[] x, int incX,  Complex[] y, int incY, [Out] Complex[] ap);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dgemm(BlasOrderType order, BlasTransType transA, BlasTransType transB, int m, int n, int k, double alpha,  double[] a, int lda,  double[] b, int ldb, double beta, [Out] double[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsymm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, int m, int n, double alpha,  double[] a, int lda,  double[] b, int ldb, double beta, [Out] double[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsyrk(BlasOrderType order, BlasUpLoType uplo, BlasTransType trans, int n, int k, double alpha,  double[] a, int lda, double beta, [Out] double[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dsyr2k(BlasOrderType order, BlasUpLoType uplo, BlasTransType trans, int n, int k, double alpha,  double[] a, int lda,  double[] b, int ldb, double beta, [Out] double[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtrmm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int m, int n, double alpha,  double[] a, int lda, [Out] double[] b, int ldb);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_dtrsm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int m, int n, double alpha,  double[] a, int lda, [Out] double[] b, int ldb);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zgemm(BlasOrderType order, BlasTransType transA, BlasTransType transB, int m, int n, int k, ref Complex alpha,  Complex[] a, int lda, Complex[] b, int ldb, ref Complex beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zsymm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, int m, int n, ref Complex alpha,  Complex[] a, int lda,  Complex[] b, int ldb, ref Complex beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zsyrk(BlasOrderType order, BlasUpLoType uplo, BlasTransType trans, int n, int k, ref Complex alpha,  Complex[] a, int lda, ref Complex beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zsyr2k(BlasOrderType order, BlasUpLoType uplo, BlasTransType trans, int n, int k, ref Complex alpha,  Complex[] a, int lda,  Complex[] b, int ldb, ref Complex beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztrmm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int m, int n, ref Complex alpha,  Complex[] a, int lda, [Out] Complex[] b, int ldb);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_ztrsm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, BlasTransType transA, BlasDiagType diag, int m, int n, ref Complex alpha,  Complex[] a, int lda, [Out] Complex[] b, int ldb);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zhemm(BlasOrderType order, BlasSideType side, BlasUpLoType uplo, int m, int n, ref Complex alpha,  Complex[] a, int lda,  Complex[] b, int ldb, ref Complex beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zherk(BlasOrderType order, BlasUpLoType uplo, BlasTransType trans, int n, int k, double alpha,  Complex[] a, int lda, double beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void cblas_zher2k(BlasOrderType order, BlasUpLoType uplo, BlasTransType trans, int n, int k, ref Complex alpha,  Complex[] a, int lda,  Complex[] b, int ldb, double beta, [Out] Complex[] c, int ldc);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGEQP3(ref int m, ref int n, [Out] double[] a, ref int lda, [Out] int[] jpvt, [Out] double[] tau, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGESVD(ref char jobu, ref char jobvt, ref int m, ref int n, [Out] double[] a, ref int lda, [Out] double[] s, [Out] double[] u, ref int ldu, [Out] double[] vt, ref int ldvt, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGETRF(ref int m, ref int n, [Out] double[] a, ref int lda, [Out] int[] ipiv, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGETRI(ref int n, [Out] double[] a, ref int lda,  int[] ipiv, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGETRS(ref char trans, ref int n, ref int nrhs,  double[] a, ref int lda,  int[] ipiv, [Out] double[] b, ref int ldb, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DORGQR(ref int m, ref int n, ref int k, [Out] double[] a, ref int lda,  double[] tau, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DORMQR(ref char side, ref char trans, ref int m, ref int n, ref int k,  double[] a, ref int lda,  double[] tau, [Out] double[] c, ref int ldc, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DPOTRF(ref char uplo, ref int n, [Out] double[] a, ref int lda, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DPOTRI(ref char uplo, ref int n, [Out] double[] a, ref int lda, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DPOTRS(ref char uplo, ref int n, ref int nrhs, [Out] double[] a, ref int lda, [Out] double[] b, ref int ldb, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGEQP3(ref int m, ref int n, [Out] Complex[] a, ref int lda, [Out] int[] jpvt, [Out] Complex[] tau, [Out] Complex[] work, ref int lwork, [Out] double[] rwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGETRF(ref int m, ref int n, [Out] Complex[] a, ref int lda, [Out] int[] ipiv, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGETRI(ref int n, [Out] Complex[] a, ref int lda,  int[] ipiv, [Out] Complex[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGETRS(ref char trans, ref int n, ref int nrhs,  Complex[] a, ref int lda,  int[] ipiv, [Out] Complex[] b, ref int ldb, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZPOTRF(ref char uplo, ref int n, [Out] Complex[] a, ref int lda, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZPOTRI(ref char uplo, ref int n, [Out] Complex[] a, ref int lda, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZPOTRS(ref char uplo, ref int n, ref int nrhs, [Out] Complex[] a, ref int lda, [Out] Complex[] b, ref int ldb, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZUNGQR(ref int m, ref int n, ref int k, [Out] Complex[] a, ref int lda,  Complex[] tau, [Out] Complex[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZUNMQR(ref char side, ref char trans, ref int m, ref int n, ref int k,  Complex[] a, ref int lda,  Complex[] tau, [Out] Complex[] c, ref int ldc, [Out] Complex[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGESVD(ref char jobu, ref char jobvt, ref int m, ref int n, [Out] Complex[] a, ref int lda, [Out] double[] s, [Out] Complex[] u, ref int ldu, [Out] Complex[] v, ref int ldvt, [Out] Complex[] work, ref int lwork, [Out] double[] rwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGELS(ref char trans, ref int m, ref int n, ref int nrhs, [Out] Complex[] a, ref int lda, [Out] Complex[] b, ref int ldb, [Out] Complex[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGELS(ref char trans, ref int m, ref int n, ref int nrhs, [Out] double[] a, ref int lda, [Out] double[] b, ref int ldb, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGELSS(ref int m, ref int n, ref int nrhs, [Out] double[] a, ref int lda, [Out] double[] b, ref int ldb, [Out] double[] s, ref double rcond, ref int rank, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGELSS(ref int m, ref int n, ref int nrhs, [Out] Complex[] a, ref int lda, [Out] Complex[] b, ref int ldb, [Out] double[] s, ref double rcond, ref int rank, [Out] Complex[] work, ref int lwork, [Out] double[] rwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGELSY(ref int m, ref int n, ref int nrhs, [Out] double[] a, ref int lda, [Out] double[] b, ref int ldb, [Out] int[] jpvt, ref double rcond, ref int rank, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGELSY(ref int m, ref int n, ref int nrhs, [Out] Complex[] a, ref int lda, [Out] Complex[] b, ref int ldb, [Out] int[] jpvt, ref double rcond, ref int rank, [Out] Complex[] work, ref int lwork, [Out] double[] rwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double DLANGE(ref char norm, ref int m, ref int n,  double[] a, ref int lda,  double[] work);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern double ZLANGE(ref char norm, ref int m, ref int n,  Complex[] a, ref int lda,  double[] work);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGECON(ref char norm, ref int n,  double[] a, ref int lda, ref double anorm, ref double rcond,  double[] work,  int[] iwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGECON(ref char norm, ref int n,  Complex[] a, ref int lda, ref double anorm, ref double rcond,  Complex[] work,  double[] rwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern int ilaenv(ref int ispec, string name, string opts, ref int n1, ref int n2, ref int n3, ref int n4, ref short name_len, ref short opts_len);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void DGEQRF(ref int m, ref int n, [Out] double[] a, ref int lda, [Out] double[] tau, [Out] double[] work, ref int lwork, ref int info);

        [DllImport(dllName, ExactSpelling = true, SetLastError = false, CallingConvention = CallingConvention.Cdecl)]
        internal static extern void ZGEQRF(ref int m, ref int n, [Out] Complex[] a, ref int lda, [Out] Complex[] tau, [Out] Complex[] work, ref int lwork, ref int info);

        #endregion PInvoke calls
    }
}